package yxy.cherry.battle.module.affix.skill;

import yxy.cherry.battle.Battle;
import yxy.cherry.battle.Fighter;
import yxy.cherry.battle.module.affix.AffixContext.AffixTarget;
import yxy.cherry.battle.module.affix.AffixFactory.SkillTypeEnum;
import yxy.cherry.battle.module.affix.Skill;
import yxy.cherry.battle.module.affix.SkillType;
import yxy.cherry.data.bean.skill;

/**
 * 援护
 */
@SkillType(Type = SkillTypeEnum.援护)
public class Affix_08_援护 extends Skill {

    public Affix_08_援护(Fighter actor, skill skilldata, int probability, int[] params) {
        super(actor, skilldata, probability, params);
    }

    @Override
    public boolean exec(Fighter actor, Fighter target) {
        if (getAffixContext().is附加行动中()) {
            return false;
        }
        if (!actor.is活着()) {
            return false;
        }

        if (getAffixContext().get攻击行动().getTarget(actor) != null) {
            return false;
        }
        AffixTarget protection = null;
        //目标全为满血特殊处理只选择血量最少的
        boolean isFullBlood = true;
        for (AffixTarget affixTarget : getAffixContext().get攻击行动().get目标们()) {
            if(affixTarget.get目标().get血量万分值() != VALUE_比率分母)isFullBlood = false;
        }
        if(isFullBlood){
            for (AffixTarget affixTarget : getAffixContext().get攻击行动().get目标们()) {
                if (protection == null || affixTarget.get目标().get血量() < protection.get目标().get血量()) {
                    protection = affixTarget;
                }
            }
        }else{
            for (AffixTarget affixTarget : getAffixContext().get攻击行动().get目标们()) {
                if (protection == null || affixTarget.get目标().get血量万分值() < protection.get目标().get血量万分值()) {
                    protection = affixTarget;
                }
            }
        }
        //混乱状态情况特殊处理
        String protectionName = protection.get目标().getFighterPosition().name();
        String subsName1 = protectionName.substring(protectionName.length() - 1);
        String actorName = actor.getFighterPosition().name();
        String subsName2 = actorName.substring(actorName.length() - 1);
        if(!subsName1.equals(subsName2)){
            return false;
        }
        getAffixContext().get攻击行动().set援护(protection, actor);

        Battle.battleLogger(6, "援护：援护者：" + protection.get目标().getName() + "援护对象：" + actor.getName());

        return true;
    }
}